package dynamicFields;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import Models.Recipe;

public class EntryList extends JPanel {
	public static final String BASIC = "Basic"; //used for basic fields that all recipes have
	public static final String READ = "Read"; //read only
	
	public static final String[] titles = {"ID:", "Name:", "Style:", "OG:", "FG:", "IBU:", "Pre Boil Vol(gal):", "Post Boil Vol(gal):",
			"Boil Time:", "Mash Temp(F):", "Mash Time:", "Efficiency:", "Brew Date:"};

	private String type;
	private Recipe rec;
	
	ArrayList<Entry> entries;
	
	public EntryList(String type) {
		this.type = type;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		entries = new ArrayList<Entry>();
		if(type.equals(BASIC)) {
			generateBasicEntries();
		} else if(type.equals(READ)) {
			generateReadEntries();
		}
	}
	
	private void addItem(Entry entry) {
		entries.add(entry);
		this.add(entry);
	}
	
	/**
	 * Populates the contents labels to contain the correct info about the recipe.
	 * can be used on basic EntryLists & read only EntryLists
	 * @param contents
	 */
	public void updateBasicContents(Recipe rec) {
		if(this.type.equals(BASIC)) {
			((BasicEntry)this.entries.get(0)).setText(rec.getIdRecipe());
			((BasicEntry)this.entries.get(1)).setText(rec.getName());
			((BasicEntry)this.entries.get(2)).setText(rec.getStyle());
			((BasicEntry)this.entries.get(3)).setText(rec.getOG());
			((BasicEntry)this.entries.get(4)).setText(rec.getFG());
			((BasicEntry)this.entries.get(5)).setText(rec.getIBU());
			((BasicEntry)this.entries.get(6)).setText(rec.getPreBoilVol());
			((BasicEntry)this.entries.get(7)).setText(rec.getPostBoilVol());
			((BasicEntry)this.entries.get(8)).setText(rec.getBoilTime());
			((BasicEntry)this.entries.get(9)).setText(rec.getMashTemp());
			((BasicEntry)this.entries.get(10)).setText(rec.getMashTime());
			((BasicEntry)this.entries.get(11)).setText(rec.getEfficiency());
			((BasicEntry)this.entries.get(12)).setText(rec.getBrewDate());
		}
		if(this.type.equals(READ)) {
			((ReadEntry)this.entries.get(0)).setText(rec.getIdRecipe());
			((ReadEntry)this.entries.get(1)).setText(rec.getName());
			((ReadEntry)this.entries.get(2)).setText(rec.getStyle());
			((ReadEntry)this.entries.get(3)).setText(rec.getOG());
			((ReadEntry)this.entries.get(4)).setText(rec.getFG());
			((ReadEntry)this.entries.get(5)).setText(rec.getIBU());
			((ReadEntry)this.entries.get(6)).setText(rec.getPreBoilVol());
			((ReadEntry)this.entries.get(7)).setText(rec.getPostBoilVol());
			((ReadEntry)this.entries.get(8)).setText(rec.getBoilTime());
			((ReadEntry)this.entries.get(9)).setText(rec.getMashTemp());
			((ReadEntry)this.entries.get(10)).setText(rec.getMashTime());
			((ReadEntry)this.entries.get(11)).setText(rec.getEfficiency());
			((ReadEntry)this.entries.get(12)).setText(rec.getBrewDate());	
		}
	}
	
	
	private void generateBasicEntries() {
		int length = titles.length;
		for(int i = 0; i < length; i++) {
			addItem(new BasicEntry(this, titles[i]));
		}
	}
	
	private void generateReadEntries() {
		int length = titles.length;
		for(int i = 0; i < length; i++) {
			addItem(new ReadEntry(this, titles[i]));
		}
	}
	
	/**
	 * Removes the entry from the list
	 * @param entry
	 */
	public void removeItem(Entry entry) {
		 entries.remove(entry);
		 remove(entry);
		 revalidate();
	}
}
